import Vue from 'vue'
import Message from './message.vue'
import Confirm from './confirm.vue'
import Loading from './loading.vue'

let list = {}

let SwMessage = (msg, time, cb, modalClass) => {
  let options = typeof msg === 'object' ? msg : {}
  let Vm = Vue.extend(Message)
  let component = new Vm({
    data: {
      msg: options.msg || msg || 'msg',
      time: options.time || time || 3000,
      modalClass: options.modalClass || modalClass || ''
    },
    methods: {
      cb: options.cb || cb || (() => {})
    }
  }).$mount()
  document.querySelector('body').appendChild(component.$el)
  return component
}

let SwConfirm = (options = {}) => {
  // 同一个页面多次弹出相同id则重新打开
  if (options.id && list[options.id]) {
    list[options.id].reopen(true)
    return list[options.id]
  }

  let Vm = Vue.extend(Confirm)
  let component = new Vm({
    data: {
      title: options.title,
      content: options.content,
      okText: options.okText,
      noText: options.noText,
      zIndex: options.zIndex || global.swModalZIndex,
      isShowBg: options.isShowBg || false,
      isShowCloseBtn: options.isShowCloseBtn || false,
      isCloseOnClickBg: options.isCloseOnClickBg || false,
      modalClass: options.modalClass
    },
    methods: {
      no: options.no || (() => { component.close() }),
      ok: options.ok || (() => { component.close() })
    }
  }).$mount()
  document.querySelector('body').appendChild(component.$el)

  if (options.id) {
    list[options.id] = component
  }
  return component
}

let SwLoading = (options = {}) => {
  // 同一个页面多次弹出相同id则重新打开
  if (options.id && list[options.id]) {
    list[options.id].reopen(true)
    return list[options.id]
  }

  let Vm = Vue.extend(Loading)
  let component = new Vm({
    data: {
    },
    methods: {
    }
  }).$mount()
  document.querySelector('body').appendChild(component.$el)

  if (options.id) {
    list[options.id] = component
  }
  return component
}

export default {
  message: (msg, time, cb, modalClass) => {
    return SwMessage(msg, time, cb, modalClass)
  },
  confirm: (options) => {
    return SwConfirm(options)
  },
  loading: (options) => {
    return SwLoading(options)
  },
  destroyAll (undestroy = true) {
    let parent
    let modalWraps = document.querySelectorAll('.modalWrap')
    for (let i = 0; i < modalWraps.length; i++) {
      if (undestroy) {
        if (modalWraps[i] && modalWraps[i].className.indexOf('undestroy') === -1) {
          parent = modalWraps[i].parentNode
          parent.removeChild(modalWraps[i])
        }
      } else {
        parent = modalWraps[i].parentNode
        parent.removeChild(modalWraps[i])
      }
    }

    // let modals = document.querySelectorAll('.modal')
    // for (let j = 0; j < modals.length; j++) {
    //   parent = modals[j].parentNode
    //   if (undestroy) {
    //     if (modals[j] && modals[j].className.indexOf('undestroy') === -1) {
    //       parent = modals[j].parentNode
    //       parent.removeChild(modals[j])
    //     }
    //   } else {
    //     parent = modals[j].parentNode
    //     parent.removeChild(modals[j])
    //   }
    // }
    list = {}
  }
}
